package com.shensi.adminCollect.controller.station;


import com.alibaba.fastjson.JSONObject;
import com.shensi.adminCollect.common.constant.HttpStatus;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.PageDomain;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.common.core.page.TableSupport;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.station.NonOilOrder;
import com.shensi.adminCollect.domain.station.OrderDetails;
import com.shensi.adminCollect.service.station.NonOilOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (NonOilOrder)表控制层
 *
 * @author makejava
 * @since 2023-08-16 17:46:22
 */
@RestController
@RequestMapping("/nonOilOrder")
@Slf4j
@Api("非油品订单接口")
public class NonOilOrderController extends BaseController {

    @Resource
    private NonOilOrderService nonOilOrderService;

    /**
     * 分页查询非油订单
     *
     * @param nonOilOrder 筛选条件
     * @return 查询结果
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询非油订单",httpMethod = "POST")
    public TableDataInfo selectNonOilOrderList(@RequestBody NonOilOrder nonOilOrder) {
        if(Objects.isNull(nonOilOrder) || Objects.isNull(nonOilOrder.getPageNum())
                || Objects.isNull(nonOilOrder.getPageSize())){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("请求参数异常");
            rspData.setRows(null);
            rspData.setTotal(0);
            return rspData;
        }
        log.info("selectNonOilOrderList 非油订单请求参数：[{}]", JSONObject.toJSONString(nonOilOrder));
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageNum(nonOilOrder.getPageNum());
        pageDomain.setPageSize(nonOilOrder.getPageSize());
        startPage(pageDomain);
        List<NonOilOrder> nonOilOrders = nonOilOrderService.queryList(nonOilOrder);
        return getDataTable(nonOilOrders);
    }


    @PostMapping("/todayStatisticsData")
    @ApiOperation("查询今日订单统计")
    public AjaxResult getTodayStatisticsData(@RequestBody NonOilOrder nonOilOrder) {
        return AjaxResult.success(nonOilOrderService.getTodayStatisticsData(nonOilOrder));
    }

    @PostMapping("/export")
    @ApiOperation("导出")
    public AjaxResult export(@RequestBody NonOilOrder nonOilOrder){
        log.info("export 非油订单导出参数：[{}]", JSONObject.toJSONString(nonOilOrder));
        List<NonOilOrder> query = nonOilOrderService.query(nonOilOrder);
        ExcelUtil<NonOilOrder> util = new ExcelUtil<NonOilOrder>(NonOilOrder.class);
        return util.exportExcel(query, "NonOilOrder");
    }


    /**
     * 新增数据
     *
     * @param nonOilOrder 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    public AjaxResult add(@RequestBody NonOilOrder nonOilOrder) {
        return AjaxResult.success(nonOilOrderService.insert(nonOilOrder));
    }

    /**
     * 编辑数据
     *
     * @param nonOilOrder 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "编辑数据",httpMethod = "POST")
    public AjaxResult edit(@RequestBody NonOilOrder nonOilOrder) {
        if(Objects.isNull(nonOilOrder) || StringUtils.isEmpty(nonOilOrder.getOrderCode())){
            return AjaxResult.error("请求参数异常");
        }
        log.info("edit 修改非油订单：[{}]",JSONObject.toJSONString(nonOilOrder));
        int i = nonOilOrderService.update(nonOilOrder);
        if(i <= 0){
            return AjaxResult.error("修改非油订单失败");
        }
        return AjaxResult.success();
    }

    /**
     * 查询订单详情
     * */
    @GetMapping("/order/details")
    @ApiOperation(value = "查询订单详情",httpMethod = "GET")
    public AjaxResult selectOrderDetails(@RequestParam("orderCode") String orderCode){
        log.info("selectOrderDetails 查询订单详情请求参数: [{}]",orderCode);
        List<OrderDetails> orderDetails = nonOilOrderService.selectListByOrderCode(orderCode);
        return AjaxResult.success(orderDetails);
    }


    /**
     * 订单详情导出
     * */
    @GetMapping("/order/details/export")
    @ApiOperation(value = "查询订单详情",httpMethod = "GET")
    public AjaxResult orderDetailsExport(@RequestParam("orderCode") String orderCode){
        log.info("selectOrderDetails 查询订单详情请求参数: [{}]",orderCode);
        List<OrderDetails> orderDetails = nonOilOrderService.selectListByOrderCode(orderCode);
        ExcelUtil<OrderDetails> util = new ExcelUtil<OrderDetails>(OrderDetails.class);
        return util.exportExcel(orderDetails, "NonOilOrderDetails");
    }


}

